<?php

require_once 'SecurityController.php';

class LoginController extends SecurityController
{

    public function init()
    {
        parent::init();
    }

    public function indexAction()
    {
        if ($this->getUser() != null) {
            $this->_redirect('/');
        }
    }

    public function validateAction()
    {

        $email = $this->getRequest()->getParam('email');
        $password = $this->getRequest()->getParam('password');
        //$user_table = new Application_Model_DbTable_Usuario();

        $adapter = new Zend_Auth_Adapter_DbTable
        (Zend_Db_Table::getDefaultAdapter(), 'usuario', 'correo', 'password');

        $adapter->setIdentity($email);
        $adapter->setCredential($password);

        $auth = Zend_Auth::getInstance()->authenticate($adapter);
        if ($auth->isValid()) {
            $row = $adapter->getResultRowObject(array('id', 'correo', 'perfil'));
            $mapper = new Application_Model_UsuarioMapper();
            $usuario = $mapper->map($row);

            $storage = new Zend_Auth_Storage_Session();
            $storage->write($usuario);

            $this->initializeAction();
            $this->flashMessage('Sesion iniciada');
            $this->_redirect('/');

        } else {
            $this->flashMessage('Los datos son incorrectos', false);
            $this->_redirect('/login');
        }
    }

    public function deniedAction()
    {
        if ($this->getUser() == null) {
//        if ($_SESSION['user'] = null) {
            $this->_redirect('/login');
        }
    }

    public function logoutAction()
    {
        $this->cleanAction();
        $this->flashMessage('Sesion cerrada');
        $this->_redirect('/login');
    }


}







